package org.jeecg.modules.data.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.data.entity.TDataReport;
import org.jeecg.modules.data.entity.TDataStatistics;
import org.jeecg.modules.data.entity.TInvestigationRelease;
import org.jeecg.modules.data.mapper.TDataReportMapper;
import org.jeecg.modules.data.service.ITDataReportService;
import org.jeecg.modules.data.service.ITDataStatisticsService;
import org.jeecg.modules.data.service.ITInvestigationReleaseService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: t_data_report
 * @Author: jeecg-boot
 * @Date:   2025-05-20
 * @Version: V1.0
 */
@Service
public class TDataReportServiceImpl extends ServiceImpl<TDataReportMapper, TDataReport> implements ITDataReportService {
    @Resource
    private ISysDictService sysDictService;
    @Resource
    private ITDataStatisticsService tDataStatisticsService;
    @Resource
    private ITInvestigationReleaseService tInvestigationReleaseService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReportAndStatis(TDataReport tDataReport, TDataStatistics statistics) {
        // 保存报表
        this.save(tDataReport);
        // 保存统计
        this.saveOrUpStatis(statistics, tDataReport.getTown());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateReportAndStatis(TDataReport tDataReport, TDataStatistics statistics) {
        // 保存报表
        this.updateById(tDataReport);
        // 保存统计
        this.saveOrUpStatis(statistics, tDataReport.getTown());
    }

    @Override
    public List<TDataReport> selectAreaList(TDataReport report) {
        return this.baseMapper.selectAreaList(report);
    }

    @Override
    public void deleteAndStatis(String id) {
        TDataReport tDataReport = this.getById(id);
        this.removeById(id);
        // 重新计算统计数据
        tDataStatisticsService.updateBatchByReport(tDataReport);
    }

    @Override
    public Page<TDataReport> selectPageList(Page<TDataReport> page, TDataReport tDataReport) {
        List<TDataReport> list = this.baseMapper.selectPageList(page, tDataReport);
        return page.setRecords(list);
    }

    /**
     * 保存或更新统计
     * @param statistics
     * @param town
     */
    private void saveOrUpStatis(TDataStatistics statistics, String town) {
        // 获取类型
        TInvestigationRelease ti = tInvestigationReleaseService.getById(statistics.getInvestigationId());
        String text = sysDictService.queryDictTextByKey("crop_type", ti.getCropType());
        // 获取年份
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(ti.getStartTime());
        int year = calendar.get(Calendar.YEAR);
        statistics.setYear(year);
        statistics.setInvestigationName(text);
        tDataStatisticsService.saveOrUpStatis(statistics);
    }
}
